package cn.miwu52.homeworkerp.Controller;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Shader;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;
import com.huawei.hms.ml.scan.HmsScanAnalyzerOptions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.miwu52.homeworkerp.R;
import cn.miwu52.homeworkerp.bean.Entrepot;
import cn.miwu52.homeworkerp.dao.EntrepotDao;
import cn.miwu52.homeworkerp.dao.ProductDao;
import cn.miwu52.homeworkerp.dao.UserDao;

public class OutEntrepotActivity extends AppCompatActivity {
    private EntrepotDao entrepotDao;
    private static final int CAMERA_REQ_CODE = 111;
    private static final int REQUEST_CODE_SCAN_ONE = 0X01;
    private ProductDao productDao;
    private String entrepotId;
    private SharedPreferences sharedPreferences;
    private UserDao userDao;

    //发起扫码请求
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Log.i("发起扫码请求", requestCode + "" + grantResults);
        if (requestCode == CAMERA_REQ_CODE && grantResults.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {

            HmsScanAnalyzerOptions hmsScanAnalyzerOptions = new HmsScanAnalyzerOptions.Creator().
                    setHmsScanTypes(HmsScan.CODE128_SCAN_TYPE, HmsScan.CODE39_SCAN_TYPE, HmsScan.CODE93_SCAN_TYPE, HmsScan.EAN13_SCAN_TYPE).create();
            ScanUtil.startScan(this, REQUEST_CODE_SCAN_ONE, hmsScanAnalyzerOptions);
        }
    }

    //接收返回值
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_SCAN_ONE) {
            HmsScan parcelableExtra = data.getParcelableExtra(ScanUtil.RESULT);
            if (parcelableExtra != null) {
                //处理出库

                if (productDao.findProductSumById(parcelableExtra.getOriginalValue().trim()) <= 0) {
                    MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.kucunbuzu);
                    mediaPlayer.start();
                    return;
                }

                boolean productById = productDao.isProductById(parcelableExtra.getOriginalValue().trim());
                if (!productById) {
                    productDao.updateProductCheckOut(parcelableExtra.getOriginalValue().trim());
                    MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.chukuchenggong);
                    mediaPlayer.start();
                    userDao.updateOutEntrepotSum(sharedPreferences.getString("userName", "userName"));
                    Toast.makeText(getApplicationContext(), "商品出库成功！", Toast.LENGTH_SHORT).show();
                } else if (!productDao.isProductByEntrepot(parcelableExtra.getOriginalValue().trim(), entrepotId)) {
                    MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.cangkucuowu2);
                    mediaPlayer.start();
                } else {
                    MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.chukushibai);
                    mediaPlayer.start();
                    Toast.makeText(getApplicationContext(), "商品ID不存在，请先新增商品！", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_out_entrepot);
        entrepotDao = new EntrepotDao(this);
        Spinner entrepotList = findViewById(R.id.entrepotList);
        Button inEntrepotButton = findViewById(R.id.outEntrepotButton);
        productDao = new ProductDao(this);
        userDao = new UserDao(this);
        sharedPreferences = getSharedPreferences("Controller.MainActivity", userInfo.MODE_PRIVATE);
        List<Entrepot> entrepotList1 = entrepotDao.findEntrepotList();
        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        for (Entrepot entrepot : entrepotList1) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("spinnerEntrepotName", entrepot.getEntrepotName());
            stringObjectHashMap.put("spinnerEntrepotId", entrepot.getId());
            maps.add(stringObjectHashMap);
        }
        String[] from = {"spinnerEntrepotName", "spinnerEntrepotId"};
        int[] to = {R.id.spinnerEntrepotName, R.id.spinnerEntrepotId};
        SimpleAdapter simpleAdapter = new SimpleAdapter(OutEntrepotActivity.this, maps, R.layout.spinner_view, from, to);
        entrepotList.setAdapter(simpleAdapter);

        inEntrepotButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TextView spinnerEntrepotId = entrepotList.getSelectedView().findViewById(R.id.spinnerEntrepotId);
                entrepotId = spinnerEntrepotId.getText().toString();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, CAMERA_REQ_CODE);
                }

            }
        });

    }
}